package example.btsppecho;

import java.util.Calendar;
import java.util.Date;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author USER
 */
public class ClientCanvas extends GameCanvas implements Runnable {

    private boolean isPlay; // Game Loop runs when isPlay is true
    private long delay; // To give thread consistency
    private int currentX, currentY; // To hold current position of the 'X'
    private int currentX2, currentY2; // To hold current position of the 'X'
    private int width; // To hold screen width
    private int height; // To hold screen height
    private int speed;
// Constructor and initialization
    MIDletApplication midlet;
    ClientForm clientForm;
    public int player = 0; //1,2,3,4
    private boolean isPlayerOk = false;
    private int status = 0; //0 = diam, 1 = maju, 2 = mundur; 3 = atas; 4 = bawah

    public ClientCanvas(MIDletApplication midlet, ClientForm clientForm) {
        super(true);
        setFullScreenMode(true);
        this.midlet = midlet;
        this.clientForm = clientForm;

        width = getWidth();
        height = getHeight();
        currentX = width / 2;
        currentY = height / 2;
        currentX2 = width / 4 / 2;
        currentY2 = height / 2;
        //delay = 500;
        delay = 1;
        speed = 1;
        //player = MIDletApplication.COUNT_PLAYER;
    }
// Automatically start thread for game loop

    public void start() {
        isPlay = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void stop() {
        isPlay = false;
    }
// Main Game Loop

    private void cobaWaktu() {
        Date d;
        d = new Date();
        Calendar c = Calendar.getInstance();
        String time;
        c.setTime(d);
        time = c.get(Calendar.HOUR_OF_DAY) + ":"
                + c.get(Calendar.MINUTE) + ":"
                + c.get(Calendar.SECOND) + ":"
                + c.get(Calendar.MILLISECOND);
        System.out.println(time);
    }

    public void run() {
        Graphics g = getGraphics();
        while (isPlay == true) {
            //cobaWaktu();
            input();
            runPlayer();
            drawScreen(g);
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ie) {
            }
        }
    }
// Method to Handle User Inputs

    private void runPlayer() {
        if (status == 0) { // center
        }
        if (status == 1) { // right
            currentX = Math.min(width, currentX + speed);
        }
        if (status == 2) { //left
            currentX = Math.max(0, currentX - speed);
        }
        if (status == 3) { //up
            currentY = Math.max(0, currentY - speed);
        }
        if (status == 4) { //down
            currentY = Math.min(height, currentY + speed);
        }
    }

    private void input() {

        int keyStates = getKeyStates();
// Left
        if ((keyStates & LEFT_PRESSED) != 0 && status!=2) {
            status = 2;
            System.out.println("left " + currentX);
        }
// Right
        if ((keyStates & RIGHT_PRESSED) != 0 && status!=1) {
            if (currentX + 5 < width) {
                status = 1;
                System.out.println("right " + currentX);
            }
        }
// Up
        if ((keyStates & UP_PRESSED) != 0 && status!=3) {
            status = 3;
            System.out.println("up " + currentX);
        }
// Down
        if ((keyStates & DOWN_PRESSED) != 0 && status!=4) {
            if (currentY + 10 < height) {
                status = 4;
                System.out.println("down " + currentX);
            }
        }
    }
// Method to Display Graphics

    private String formatString(int i, int j) {
        String s = null;
        if (j == 3) {
            if (i < 10) {
                s = "00" + String.valueOf(i);
            } else if (i < 100) {
                s = "0" + String.valueOf(i);
            } else {
                s = String.valueOf(i);
            }
        }
        return s;
    }

    private void drawScreen(Graphics g) {
        //clientForm.sendMessage(clientForm.getAddress());
        /*
        if (!isPlayerOk) {
        if (clientForm.getPlayer() == 1) {
        MIDletApplication.COUNT_PLAYER = MIDletApplication.COUNT_PLAYER + 1;
        player = MIDletApplication.COUNT_PLAYER;
        isPlayerOk = true;
        }
        }
         * 
         */
        System.out.println(midlet.getCOUNT_PLAYER());

        String str = String.valueOf(player) + formatString(currentX, 3) + formatString(currentY, 3);
        ///clientForm.sendMessage(MIDletApplication.DATA_GAME + str);
        //clientForm.sendMessage(str);

        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0x0000ff);

        //g.drawString("P1 " + MIDletApplication.DATA_GAME, currentX, currentY, Graphics.TOP | Graphics.LEFT);
        g.drawString("P1 ", currentX, currentY, Graphics.TOP | Graphics.LEFT);

        g.setColor(0xff00ff);
        currentX2 = currentX / 4;
        g.drawString("^", currentX2, currentY2, Graphics.TOP | Graphics.LEFT);

        g.setColor(0xff0000);
        g.drawRect(0, 0, width / 4, getHeight());
        flushGraphics();
    }

    /*
    public void paint(Graphics g) {
    super.paint(g);
    setFullScreenMode(true);
    }
     * 
     */
    protected void pointerPressed(int x, int y) {
        this.currentX = x;
        this.currentY = y;
        //input();
    }

    protected void pointerDragged(int x, int y) {
        this.currentX = x;
        this.currentY = y;
        //input();
    }

    protected void pointerReleased(int x, int y) {
        //this.currentX = x;
        //this.currentY = y;        
        //input();
        //scroll
    }
}
